import { CommonModule } from '@/store/modules/common'
import { commonAction, getCurrentDateTime } from '@/utils/common'
// 会话状态数据
let ststusData: any = []
// 自定义状态id列表
let userStatus: any = []

// 获取会话状态数据
const getData = async () => {
  if (ststusData.length > 0) {
    return
  }
  const data = await commonAction({ url: '/im-center/action/user/status/config/query', method: 'POST' })
  ststusData = data.data
  userStatus = ststusData.filter(v => !v.isDefault).map(v => v._id)
}

// 获取当前日期
// 获取当前日期
export const today = () => {
  const date = getCurrentDateTime().split(' ')[0]
  return [date + ' 00:00:00', date + ' 23:59:59']
}

// 改变毫秒时间格式为00:00:00
export const getTime = (val) => {
  if (!val) {
    return '00:00:00'
  }
  val = Math.round(val / 1000)
  let hour: any = Math.floor(val / 3600)
  hour = hour > 9 ? hour : '0' + hour
  let min: any = Math.floor((val % 3600) / 60)
  min = min > 9 ? min : '0' + min
  let second: any = val % 60
  second = second > 9 ? second : '0' + second
  return hour + ':' + min + ':' + second
}

// 通过工号获取座席名称
export const getAgentName = (agentNumber) => {
  const agent = (CommonModule as any).publicCache.userList.filter(v => v.agentNumber === agentNumber + '')
  return agent[0] ? `${agent[0].agentName} 【${agent[0].agentNumber}】` : ''
}

/**
 * 获取自定义状态的表头
 * @returns count 状态次数 time 状态时常
 */
export const mixHeader = async () => {
  await getData()
  const count: any = []
  const time: any = []
  ststusData.map(v => {
    // TODO 如果以后需要分开展示自定义状态次数，可以去除此过滤条件
    if (!userStatus.includes(v._id)) {
      count.push(
        {
          label: v.name + '次数',
          prop: v._id + 'count'
        }
      )
    }
    time.push(
      {
        label: v.name + '状态总时长',
        prop: v._id + 'time',
        width: 120
      }
    )
  })
  return {
    count,
    time
  }
}

// 获取自定义状态次数总和
export const getAllStatusCount = (object) => {
  let count = 0
  for (const i in object) {
    if (userStatus.includes(i)) {
      count += object[i].count
    }
  }
  return count
}

// 通过id获取状态名称
export const getStatus = (id) => {
  const data = ststusData.filter(v => v._id === id)
  return data[0] ? data[0].name : ''
}

/**
 * 获取座席明细状态
 * login：登录 logout：登出 changeStatus：修改座席状态 modifySessionUpperLimit：修改会话上线
 */
export const getOperatStatus = (object) => {
  if (object.type === 'login') {
    return '登录'
  } else if (object.type === 'logout') {
    return '登出'
  } else if (object.type === 'changeStatus') {
    const data = ststusData.filter(v => v._id === object.statusConfigId)
    return data[0] ? data[0].name + '状态' : ''
  } else if (object.type === 'modifySessionUpperLimit') {
    return '修改会话上限'
  }
  return ''
}
